
package security.business;

import Entity.Pair;
import Entity.Profile;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import security.DAO.ProfileDAO;

/**
 *
 * @author JuanFelipe
 */
public class ProfileBusiness {
    Connection connection;
    private static String SUSSESFUL = "SUSSESFUL", FAIL = "FAIL";
    
    public ProfileBusiness(Connection connection) {
        this.connection = connection;
    }
    
    public ArrayList<Profile> getProfile() throws SQLException{
        String sql = "Select  * FROM dba_profiles";
        ArrayList<Profile> profiles = new ArrayList<>();
        try {
            ResultSet rs = new ProfileDAO( connection ).executeStatement( sql );
            while( rs.next() ){
                ArrayList<String> sl = new ArrayList<>();

                for(int i = 1 ; i <= rs.getMetaData().getColumnCount() ; i++){
                    if( rs.getString(i)==null )
                        sl.add( "(null)" );
                    else
                        sl.add( rs.getString(i) );
                }
                
                Profile aux = new Profile( sl.get(0), sl.get(1), sl.get(2)
                        , sl.get(3), sl.get(4) );
                profiles.add( aux );
            }
            
        } catch (SQLException ex) {
            System.out.println("Error: "+ex);
            throw ex;
            //Logger.getLogger(UserBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return profiles;
    }
    
    public String create(String name, List<Pair>limit) throws SQLException{
        
        StringBuilder sql = new StringBuilder("CREATE PROFILE \""+name +"\" LIMIT");
        
        for(Pair p : limit){
//            if( !p.getValue().toLowerCase().equals("default") 
//                    && !p.getValue().toLowerCase().equals("null") ){
                sql.append( " "+p.getName()+" "+p.getValue() );
//            }
        }
        
        ProfileDAO pdao = new ProfileDAO( connection );
        
        pdao.crear( sql.toString() );
        
        return SUSSESFUL;
    }

    public List<String> getProfileName() throws SQLException {
        String sql = "SELECT DISTINCT profile FROM dba_profiles ORDER BY profile";
        ArrayList<String> profiles = new ArrayList<>();
        try {
            ResultSet rs = new ProfileDAO( connection ).executeStatement( sql );
            while( rs.next() ){
                ArrayList<String> sl = new ArrayList<>();

                for(int i = 1 ; i <= rs.getMetaData().getColumnCount() ; i++){
                    if( rs.getString(i)==null )
                        sl.add( "(null)" );
                    else
                        sl.add( rs.getString(i) );
                }
                
                profiles.add( sl.get( 0 ) );
            }
            
        } catch (SQLException ex) {
            System.out.println("Error: "+ex);
            throw ex;
            //Logger.getLogger(UserBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return profiles;
    }

    public List<Profile> getProfile(String profile) throws SQLException {
        
        String sql = "Select  * FROM dba_profiles WHERE profile = \'"+profile+"\'";
        ArrayList<Profile> profiles = new ArrayList<>();
        try {
            ResultSet rs = new ProfileDAO( connection ).executeStatement( sql );
            while( rs.next() ){
                ArrayList<String> sl = new ArrayList<>();

                for(int i = 1 ; i <= rs.getMetaData().getColumnCount() ; i++){
                    if( rs.getString(i)==null )
                        sl.add( "(null)" );
                    else
                        sl.add( rs.getString(i) );
                }
                
                Profile aux = new Profile( sl.get(0), sl.get(1), sl.get(2)
                        , sl.get(3), sl.get(4) );
                profiles.add( aux );
            }
            
        } catch (SQLException ex) {
            System.out.println("Error: "+ex);
            throw ex;
            //Logger.getLogger(UserBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return profiles;
    }
    
    public void delete(String profile) throws SQLException{
        String sql = "DROP PROFILE \""+profile+"\"";
        ProfileDAO pdao = new ProfileDAO( connection );
        
        pdao.eliminar( sql );
    }
    
    public void update(String name, String resourseName,String limit) throws SQLException{
        String sql = "ALTER PROFILE \""+name+"\" LIMIT "+resourseName+" "+limit;
        ProfileDAO pdao = new ProfileDAO( connection );
        
        pdao.update( sql );
    }
}
